package com.fitness.servlet;

import com.fitness.dao.ProjectDAO;
import com.fitness.entity.Project;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        ProjectDAO projectDAO = new ProjectDAO();
        if ("list".equals(action)) {
            // 获取所有项目
            List<Project> projectList = projectDAO.getProjectList();
            // 添加调试输出
            System.out.println("项目列表大小: " + (projectList != null ? projectList.size() : "null"));
            if (projectList != null) {
                for (Project p : projectList) {
                    System.out.println("项目: " + p.getProjectName());
                }
            }
            request.setAttribute("projectList", projectList);
            request.getRequestDispatcher("WEB-INF/views/admin/projectList.jsp").forward(request, response);
        } else if ("delete".equals(action)) {
            // 删除项目
            int projectId = Integer.parseInt(request.getParameter("projectId"));
            projectDAO.deleteProject(projectId);
            response.sendRedirect("ProjectServlet?action=list");
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        // 调试：打印所有参数
        System.out.println("Action: " + action);
        request.getParameterMap().forEach((k, v) -> System.out.println(k + ": " + Arrays.toString(v)));

        if (action == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "缺少action参数");
            return;
        }
        ProjectDAO projectDAO = new ProjectDAO();
        try {
            if ("add".equals(action)) {
                String projectName = request.getParameter("projectName");
                double pricePerHour = Double.parseDouble(request.getParameter("pricePerHour"));
                String description = request.getParameter("description");

                Project project = new Project();
                project.setProjectName(projectName);
                project.setPricePerHour(pricePerHour);
                project.setDescription(description);

                boolean success = projectDAO.insertProject(project);
                System.out.println("添加项目结果: " + success);
                response.sendRedirect("ProjectServlet?action=list");
            } else {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "不支持的action: " + action);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器错误");
        }
    }
}